package db

import (
	"fmt"

	"go.mongodb.org/mongo-driver/bson"
	"starsine.cn/appTpl/cmn/config/aconf"
	"starsine.cn/appTpl/cmn/db/mongodb"
	. "starsine.cn/appTpl/cmn/defs"
)

type LogDB struct {
	db *mongodb.MongoDB
}

func CreateLogDB(appCtxt *AppContext) *LogDB {
	p := new(LogDB)

	p.db = mongodb.CreateMongoDB(appCtxt,
		fmt.Sprintf("mongodb://%s", aconf.GetString("DB.Addr", "localhost:27017", aconf.WithENV("db_addr"))),
		aconf.GetString("DB.Name", "svcLog", aconf.WithENV("db_name")),
		aconf.GetString("DB.Collection", "default", aconf.WithENV("db_collection")),
	)

	return p
}

func (p *LogDB) Start() error {
	err := p.db.ConnectDB()

	if err != nil {
		return fmt.Errorf("CreateLogDB fail, err: %s", err)
	}

	return nil
}

func (p *LogDB) GetOne(k, v string) *bson.M {
	return p.db.GetOne(k, v)
}

func (p *LogDB) InsertOne(doc interface{}) error {
	return p.db.InsertOne(doc)
}
